package tests;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

import queue.Queue;

import dropoffdesk.DropOffDesk;

import coreSimulation.Baggage;
import coreSimulation.Passenger;
import coreSimulation.PassengerList;
import exceptions.DuplicatePassengerException;

/*
 * Test the calculated excess baggage charges individually and for totals
 */
public class DropOffDeskTest {

	@Test
	public void testProcessQueue() {
		PassengerList passengerList = new PassengerList();

		Queue queue = new Queue("PassengerList.csv");

		DropOffDesk desk = new DropOffDesk(queue, passengerList);

		// instantiate passenger and bag 2x max width
		Baggage bag1 = new Baggage(180, 10, 10, 10);
		Passenger p1 = new Passenger("Passenger One", "SA-10000000");
		p1.addBaggage(bag1);

		// instantiate passenger and bag 2x max height
		Baggage bag2 = new Baggage(10, 150, 10, 10);
		Passenger p2 = new Passenger("Passenger Two", "SA-20000000");
		p2.addBaggage(bag2);

		// instantiate passenger and bag 2x max depth
		Baggage bag3 = new Baggage(10, 10, 86, 10);
		Passenger p3 = new Passenger("Passenger Three", "SA-30000000");
		p3.addBaggage(bag3);

		// instantiate passenger and bag 2x max weight
		Baggage bag4 = new Baggage(10, 10, 10, 46);
		Passenger p4 = new Passenger("Passenger Four", "SA-40000000");
		p4.addBaggage(bag4);

		// check width calculation with bag 2x max on all spatial dimensions
		Baggage bag5 = new Baggage(180, 150, 86, 10);
		Passenger p5 = new Passenger("Passenger Five", "SA-50000000");
		p5.addBaggage(bag5);

		// instantiate passenger and bag 2x max on all spatial dimensions and
		// weight
		Baggage bag6 = new Baggage(180, 150, 86, 46);
		Passenger p6 = new Passenger("Passenger Six", "SA-60000000");
		p6.addBaggage(bag6);

		// instantiate passenger and bag which matches exactly the max allowance
		// (no charge should occur)
		Baggage bag7 = new Baggage(90, 75, 43, 23);
		Passenger p7 = new Passenger("Passenger Seven", "SA-70000000");
		p7.addBaggage(bag7);

		// instantiate passenger and bag with non-integer weight
		// (1.5 times weight = 50% of EXCESS_WEIGHT_CHARGE)
		Baggage bag8 = new Baggage(90, 75, 43, 34.5);
		Passenger p8 = new Passenger("Passenger Eight", "SA-80000000");
		p8.addBaggage(bag8);

		// just over maximum values
		Baggage bag9 = new Baggage(91, 76, 44, 23.1);
		Passenger p9 = new Passenger("Passenger Nine", "SA-90000000");
		p9.addBaggage(bag9);

		// add the test passengers to the queue
		try {
			queue.add(p1);
			queue.add(p2);
			queue.add(p3);
			queue.add(p4);
			queue.add(p5);
			queue.add(p6);
			queue.add(p7);
			queue.add(p8);
			queue.add(p9);

		} catch (DuplicatePassengerException e) {
			e.printStackTrace();
		}

		// open the desk and process the test queue in a separate thread
		desk.setIsOpen(true);
		Thread thread = new Thread(desk);
		thread.start();

		// check on the queue and once the desk has processed all of the
		// passengers close the desk and release the thread
		while (queue.size() > 0) {
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		desk.setIsOpen(false);

		// test the report contains the correct calculated charges for each of
		// the test passengers
		assertTrue(p1.getExcessCharges() == 50.00);

		assertTrue(p2.getExcessCharges() == 50.00);

		assertTrue(p3.getExcessCharges() == 50.00);

		assertTrue(p4.getExcessCharges() == 70.00);

		assertTrue(p5.getExcessCharges() == 150.00);

		assertTrue(p6.getExcessCharges() == 220.00);

		assertTrue(p7.getExcessCharges() == 0);

		assertTrue(p8.getExcessCharges() == 35.00);

		assertTrue(p9.getExcessCharges() == 2.68);
		System.out.println(passengerList.getExcessCharges());

		// check that the passenger list gives the correct total
		assertTrue(passengerList.getExcessCharges() == 627.68);

	}
}
